home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / mint / editors / mjovesrc.zoo / mac.c < prev    next >
C/C++ Source or Header  |  1992-04-04  |  59KB  |  2,957 lines

  1. /***************************************************************************
  2.  * This program is Copyright (C) 1986, 1987, 1988 by Jonathan Payne.  JOVE *
  3.  * is provided to you without charge, and with no warranty.  You may give  *
  4.  * away copies of JOVE, including sources, provided that this notice is    *
  5.  * included in all the files.                                              *
  6.  ***************************************************************************/
  7.  
  8.  
  9. /* (C) 1986, 1987, 1988 Ken Mitchum. This code is intended only for use with Jove. */
  10.  
  11. #include "tune.h"
  12. #ifdef    MAC
  13. #include <MacTypes.h>
  14. #include "jove.h"
  15. #include <QuickDraw.h>
  16. #include <WindowMgr.h>
  17. #include <FontMgr.h>
  18. #include <ListMgr.h>
  19. #include <EventMgr.h>
  20. #include <ControlMgr.h>
  21. #include <DialogMgr.h>
  22. #include <ResourceMgr.h>
  23. #include <ToolboxUtil.h>
  24. #include <HFS.h>
  25. #include <StdFilePkg.h>
  26. #include <MenuMgr.h>
  27. #include <pascal.h>
  28. #include <errno.h>
  29. #include <SegmentLdr.h>
  30. #include "mac.h"
  31. #include "termcap.h"
  32.  
  33. extern struct menu Menus[NMENUS];
  34.  
  35. private    EventRecord the_Event;
  36.  
  37. private void SetBounds proto((void));
  38. private void Set_std proto((void));
  39. private void Reset_std proto((void));
  40. private bool is_dir proto((char *));
  41. private bool findtext proto((void));
  42.  
  43. /* keycodes (from Inside MacIntosh I-251). because of changes with
  44. the MacPlus, there are some duplicate codes between cursor keys and
  45. keypad keys. these can be deciphered by the corresponding character
  46. codes, which are different. this table simply translates a keycode
  47. into a character code that is appropriate. */
  48.  
  49. #define NOKEY (-1)
  50. #define RET 0x0D
  51. #define TAB 0x09
  52. #define BACKSP 0x08
  53. #define ENTERL NOKEY    /* left enter key absent on MacPlus */
  54. #define COMMAND NOKEY    /* will be no translation anyway for these */
  55. #define SHIFT NOKEY
  56. #define CAPSLOCK NOKEY
  57. #define OPTION NOKEY
  58. #define PADDOT '.'        /* PAD period */
  59. #define PAD0 '0'
  60. #define PAD1 '1'
  61. #define PAD2 '2'
  62. #define PAD3 '3'
  63. #define PAD4 '4'
  64. #define PAD5 '5'
  65. #define PAD6 '6'
  66. #define PAD7 '7'
  67. #define PAD8 '8'
  68. #define PAD9 '9'
  69. #define LEFTCURS 'B'        /* jove only, make like commands */
  70. #define RIGHTCURS 'F'
  71. #define UPCURS 'P'
  72. #define DOWNCURS 'N'
  73. #define PADENTER RET
  74. #define PADMINUS '-'
  75. #define CLEAR 0
  76.  
  77. private char nsh_keycodes[] = {
  78.     'a','s','d','f','h',                        /* 0 - 4 */
  79.     'g','z','x','c','v',                        /* 5 - 9 */
  80.     NOKEY,'b','q','w','e',                    /* 10 - 14 */
  81.     'r','y','t','1','2',                    /* 15 - 19 */
  82.     '3','4','6','5','=',                    /* 20 - 24 */
  83.     '9','7','-','8','0',                    /* 25 - 29 */
  84.     ']','O','u','[','i',                    /* 30 - 34 */
  85.     'p',RET,'l','j','\'',                    /* 35 - 39 */
  86.     'k',';','\\',',','/',                    /* 40 - 44 */
  87.     'n','m','.',TAB,NOKEY,                    /* 45 - 49 */
  88.     '`',BACKSP,ENTERL,NOKEY,NOKEY,            /* 50 - 54 */
  89.     COMMAND,SHIFT,CAPSLOCK,OPTION, NOKEY,    /* 55 - 59 */
  90.     NOKEY,NOKEY,NOKEY,NOKEY,NOKEY,            /* 60 - 64 */
  91.     PADDOT,RIGHTCURS,NOKEY,NOKEY,NOKEY,        /* 65 - 69 */
  92.     LEFTCURS,CLEAR,DOWNCURS,NOKEY,NOKEY,    /* 70 - 74 */
  93.     NOKEY,PADENTER,UPCURS,PADMINUS,NOKEY,    /* 75 - 79 */
  94.     NOKEY,NOKEY,PAD0,PAD1,PAD2,                /* 80 - 84 */
  95.     PAD3,PAD4,PAD5,PAD6,PAD7,                /* 85 - 89 */
  96.     NOKEY,PAD8,PAD9
  97. };
  98.  
  99. private char sh_keycodes[] = {
  100.     'A','S','D','F','H',                        /* 0 - 4 */
  101.     'G','Z','X','C','V',                        /* 5 - 9 */
  102.     NOKEY,'B','Q','W','E',                    /* 10 - 14 */
  103.     'R','Y','T','!','@',                    /* 15 - 19 */
  104.     '#','$','^','%','+',                    /* 20 - 24 */
  105.     '(','&','_','*',')',                    /* 25 - 29 */
  106.     '}','O','U','{','I',                    /* 30 - 34 */
  107.     'P',RET,'L','J','\'',                    /* 35 - 39 */
  108.     'K',';','|','<','?',                    /* 40 - 44 */
  109.     'N','M','>',TAB,NOKEY,                    /* 45 - 49 */
  110.     '~',BACKSP,ENTERL,NOKEY,NOKEY,            /* 50 - 54 */
  111.     COMMAND,SHIFT,CAPSLOCK,OPTION, NOKEY,    /* 55 - 59 */
  112.     NOKEY,NOKEY,NOKEY,NOKEY,NOKEY,            /* 60 - 64 */
  113.     PADDOT,RIGHTCURS,NOKEY,NOKEY,NOKEY,        /* 65 - 69 */
  114.     LEFTCURS,CLEAR,DOWNCURS,NOKEY,NOKEY,    /* 70 - 74 */
  115.     NOKEY,PADENTER,UPCURS,PADMINUS,NOKEY,    /* 75 - 79 */
  116.     NOKEY,NOKEY,PAD0,PAD1,PAD2,                /* 80 - 84 */
  117.     PAD3,PAD4,PAD5,PAD6,PAD7,                /* 85 - 89 */
  118.     NOKEY,PAD8,PAD9
  119. };
  120.  
  121.  
  122.  
  123. /* tn.h Modified for variable screen size 11/21/87. K. Mitchum */
  124.  
  125. #define SCREENSIZE (wc->w_rows * ROWSIZE)
  126. #define FONT monaco
  127. #define TEXTSIZE 9
  128.  
  129. #define HEIGHT 11
  130. #define WIDTH 6
  131. #define DESCENT 2
  132. #define TWIDTH CO * WIDTH
  133. #define THEIGHT LI * HEIGHT
  134.  
  135. /* window specs */
  136.  
  137. #define SCROLLWIDTH 16    /* width of scroll bar control in pixels */
  138. #define WINDWIDTH (wc->w_width - SCROLLWIDTH + 1)    /* local coordinates */
  139. #define WINDHEIGHT (wc->w_height)    /* local coordinates */
  140. #define MAXROW (LI - 1)
  141. #define MAXCOL (CO - 1)
  142.  
  143.  
  144. /* for keyboard routines */
  145. #define MCHARS 32    /* must be power of two */
  146. #define NMASK MCHARS (-1)    /* circular buffer */
  147.  
  148.  
  149. /***************************************************/
  150.  
  151. /* these normally reside in "tune.c" which we don't use */
  152.  
  153. char *CmdDb;    /* see InitMac() */
  154. char *p_tempfile = ".jrecXXX";
  155. char *d_tempfile = ".joveXXX";
  156. char *Joverc = ".joverc";
  157.  
  158.  
  159. void putcurs(),curset(),putp(),dellines(),inslines();
  160.  
  161. private Rect LimitRect;    /* bounds we can't move past */
  162.  
  163. struct wind_config {
  164.     int w_width;    /* pixel width of the Mac window */
  165.     int    w_height;
  166.     int    w_rows;    /* rows of characters which fit the window */
  167.     int    w_cols;
  168. } wc_std, wc_user, *wc;
  169.  
  170. private WindowPtr theScreen;
  171.  
  172. int
  173.     errno;
  174.  
  175. bool
  176.     Windchange,
  177.     EventCmd,
  178.     Keyonly,
  179.     Bufchange,
  180.     Modechange,
  181.     Macmode = OFF;
  182.  
  183. /* Initialization Routines. */
  184.  
  185. void
  186. InitBinds()
  187. {
  188.     struct cmd *c;
  189.     data_obj **p;
  190.     int i;
  191.  
  192.     p = MainKeys;
  193.     for (i= 0; i < NCHARS; i++) {
  194.         c = (struct cmd *) *p;
  195.         c->c_map = F_MAINMAP;
  196.         c->c_key = i;
  197.         p++;
  198.     }
  199.  
  200.     p = EscKeys;
  201.     for (i= 0; i < NCHARS; i++) {
  202.         c = (struct cmd *) *p;
  203.         c->c_map = F_PREF1MAP;
  204.         c->c_key = i;
  205.         p++;
  206.     }
  207.     p = CtlxKeys;
  208.     for (i= 0; i < NCHARS; i++) {
  209.         c = (struct cmd *) *p;
  210.         c->c_map = F_PREF2MAP;
  211.         c->c_key = i;
  212.         p++;
  213.     }
  214.  
  215. }
  216.  
  217. private    WindowPtr window;
  218. private    Rect r;
  219. private CursHandle cross;
  220.  
  221. void
  222. InitEvents()
  223. {
  224.     void InitSysMenu();
  225.  
  226.     window = theScreen;
  227.     InitSysMenu();
  228.     SetRect(&r,window->portRect.left,
  229.     window->portRect.top,
  230.     window->portRect.right - SCROLLWIDTH,
  231.     window->portRect.bottom - SCROLLWIDTH);
  232.     cross = GetCursor(crossCursor);
  233. }
  234.  
  235. void
  236. MacInit()
  237. {
  238.     char *gethome();
  239.     void tn_init();
  240.  
  241.     tn_init();
  242.     getdir();
  243.     gethome();    /* before anyone changes it */
  244.     CmdDb = malloc(strlen(gethome()) + 10);
  245.     /* ??? better check for CmdDb == NULL -- DHR */
  246.     strcpy(CmdDb,gethome());
  247.     strcat(CmdDb,"/cmds.doc");
  248.     InitBinds();
  249. }
  250.  
  251.  
  252. /* dummy routines. */
  253.  
  254. int dummy() {}
  255.  
  256. SIGRESULT
  257. (*signal(sig,func)) proto((int))
  258. int sig;
  259. SIGRESULT (*func) proto((int));
  260. {
  261.     return &dummy;
  262. }
  263.  
  264. void dorecover() {}
  265.  
  266.  
  267. /* Surrogate unix-style file i/o routines for Jove. These replace the
  268.    routines distributed in the libraries. They work with Jove, but may
  269.    not be general enough for other purposes. */
  270.  
  271. #include <io.h>
  272. #define NFILES 10
  273.  
  274. /* #define fsetup(p) { \
  275.  *    (p).ioCompletion = 0; \
  276.  *    (p).ioVRefNum = cur_vol; \
  277.  *    (p).ioDirID = cur_dir; \
  278.  *    (p).ioFVersNum = 0; \
  279.  * }
  280.  * #define isetup(p) {(p).ioCompletion = 0; (p).ioVRefNum = cur_vol;}
  281.  */
  282.  
  283. private int cur_vol;    /* Disk or volume number */
  284. private long cur_dir;    /* Directory number */
  285. private int cur_vref;    /* ugh.. Vref for volume + directory */
  286.  
  287. struct ftab {
  288.     int inuse;    /* 0 = closed 1 = binary 2 = text*/
  289.     int refnum;    /* Mac file reference number */
  290. } ft[NFILES];
  291.  
  292. private void
  293. fsetup(p)
  294. HParmBlkPtr p;
  295. {
  296.     byte_zero(p,sizeof(HParamBlockRec));
  297.     p->fileParam.ioVRefNum = cur_vol;
  298.     p->fileParam.ioDirID = cur_dir;
  299.     p->fileParam.ioFVersNum = 0;
  300. }
  301.  
  302. private void
  303. isetup(p)
  304. HIOParam *p;
  305. {
  306.     byte_zero(p,sizeof(HIOParam));
  307.     p->ioVRefNum = cur_vol;
  308. }
  309.  
  310.  
  311. /* Kludge to convert Macintosh error codes to something like Unix. */
  312.  
  313. private int
  314. cvt_err(err)    /* some of these don't make sense... */
  315. int    err;
  316. {
  317.     switch(err) {
  318.     case noErr:    errno = 0; return 0;
  319.     case dirFulErr:
  320.     case dskFulErr:    errno = ENOSPC; break;
  321.     case nsvErr:
  322.     case mFulErr:
  323.     case tmfoErr:
  324.     case fnfErr:
  325.     default:    errno = ENOENT; break;
  326.     case ioErr:    errno = EIO; break;
  327.     case bdNamErr:
  328.     case opWrErr:
  329.     case paramErr:    errno = EINVAL; break;
  330.     case fnOpnErr:                /* dubious... */
  331.     case rfNumErr:    errno = EBADF; break;
  332.     case eofErr:                /* ditto */
  333.     case posErr:    errno = ESPIPE; break;
  334.     case wPrErr:    errno = EROFS; break;
  335.     case fLckdErr:
  336.     case permErr:    errno = EACCES; break;
  337.     case fBsyErr:    errno = EBUSY; break;
  338.     case dupFNErr:    errno = EEX